<?php
/**
 * Created by PhpStorm.
 * User: baidu
 * Date: 17/8/5
 * Time: 下午4:37
 */
namespace app\api\controller\v1;

use app\api\controller\v1\AuthBase;
use think\Controller;
use app\common\lib\exception\ApiException;
use app\common\lib\Aes;
use app\common\lib\Upload;

/**
 * 客户端auth登录权限基础类库
 * 1.每个接口(需要登录 个人中心  点赞 评论) 都需要去集
 * 2.判定access_user_token是否合法
 * 3.用户信息->>user
 */
class Comment extends AuthBase {

	/**
	 * [save 评论回复功能开发]
	 * @return [type] [description]
	 */
	public function save(){
		$data = input('post.',[]);
		//news_id content to_user_id
		//validate 校验
		$validate = validate('Comment');
		if (!$validate->scene('add')->check($data)) {
            return show(config('code.error'),$validate->getError(),[],200);
        }

		//判断这个id的新闻文章是否存在 -->ent_news
		$newsData = model('News')->get(['id' => $data['news_id']]);
	    if (empty($newsData) || $newsData['status'] != 1) {
	       return show(config('code.error'),'news error ',[],404);
	    }

		$data['user_id'] = $this->user->id;

		try {
			$conmmentId = model('Comment')->add($data);
			if ($conmmentId) {
				model('News')->where(['id' => $data['news_id']])->setInc('comment_count');
		        return show(config('code.success'),'success',[],200);
		      }else{
		        return show(config('code.error'),'失败评论',[],404);
		      }
		} catch (\Exception $e) {
			return show(config('code.error'),$e->getMessage(),[],404);
		}
		return [];
	}

	/**
	 * [read 评论列表v1]
	 * @return [type] [description]
	 */
	/*public function read(){
		// select * from ent_comment as a join ent_user as b on a.user_id = b.id and a.news_id = 2
		$newsId = input('param.id',0,'intval');
		if (empty($newsId)) {
	      return show(config('code.error'),'id inexistence',[],404);
	    }
	    $param['news_id'] = $newsId;
	    $count = model('Comment')->getNormalCommentsCountByCondition($param);

	    $this->getPageAndSize(input('param.'));
	    $comments = model('Comment')->getNormalCommentsCondition($param,$this->form,$this->size);
	    $result = ['total'=>$count,
	    	'page_num'=>ceil($count/$this->size),
	    	'list'=>$comments,
	    ];

	    return show(config('code.success'),'ok',$result,200);

	}*/

	/**
	 * [read 评论列表v2]
	 * @return [type] [description]
	 */
	public function read(){
		// select * from ent_comment as a join ent_user as b on a.user_id = b.id and a.news_id = 2
		$newsId = input('param.id',0,'intval');
		if (empty($newsId)) {
	      return show(config('code.error'),'id inexistence',[],404);
	    }

	    $param['news_id'] = $newsId;
	    $count = model('Comment')->getCountByCondition($param);

	    $this->getPageAndSize(input('param.'));
	    $comments = model('Comment')->getListsByCondition($param,$this->from,$this->size);
	    if (!empty($comments)) {
	    	foreach ($comments as $key => $value) {
	    		$userIds[] = $value['user_id'];
	    		if ($value['to_user_id']) {
	    			$userIds[] = $value['to_user_id'];
	    		}
	    	}

	    	$userIds = array_unique($userIds);
	    }
	    $userIds = model('User')->getUsersUserId($userIds);
	    if (empty($userIds)) {
	    	$userIdNames = [];
	    }else{
		    foreach ($userIds as $key => $value) {
		    	$userIdNames[$value->id] = $value;
		    }
	    }

	    $resultDatas = [];
	    foreach ($comments as $key => $value) {
	    	$resultDatas[] = [
	    		'id' => $value->id,
	    		'user_id' => $value->user_id,
	    		'to_user_id' => $value->to_user_id,
	    		'content' => $value->content,
	    		'username'=>empty($userIdNames[$value->user_id])?$userIdNames[$value->user_id]->username:'',
	    		'tousername'=>empty($userIdNames[$value->user_id])?$userIdNames[$value->to_user_id]->username:'',
	    	];
	    }
	    return show(config('code.success'),'ok',$resultDatas,200);

	}

}